package org.threegroup.service.impl;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.threegroup.entity.Goods;
import org.threegroup.mappers.GoodsMappers;
import org.threegroup.service.GoodsService;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class GoodsServiceImpl implements GoodsService {
    private GoodsMappers goodsMappers;
    private SqlSession sqlSession;
    public GoodsServiceImpl(){
        try {
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            sqlSession =sqlSessionFactory.openSession(true);
            goodsMappers = sqlSession.getMapper(GoodsMappers.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public List<Goods> queryAllGoods() {
        return goodsMappers.queryAllGoods();
    }

    @Override
    public List<Goods> queryGoodsName(String name) {
        return goodsMappers.queryGoodsName(name);
    }

    @Override
    public boolean updateGoods(String specif, String unit, Double price, String remark, Integer id) {
        return goodsMappers.updateGoods(specif, unit, price, remark, id);
    }

    @Override
    public boolean updateGoodsStock(Integer stock, Integer id) {
        return goodsMappers.updateGoodsStock(stock, id);
    }

    @Override
    public boolean deleteGoods(Integer id) {
        return goodsMappers.deleteGoods(id);
    }

    @Override
    public Integer checkGoodsIfHaveRecord(Integer id) {
        return goodsMappers.checkGoodsIfHaveRecord(id);
    }
}
